<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .box {
            width: 300px;
            border: 1px solid #000;
            margin: auto;
        }
        .main{
            width: 300px;
            border: 1px solid #000;
            margin: auto;
            
        }

        .box>.head {
            display: flex;
            justify-content: space-between;
        }

        .box>.head>li {
            flex-grow: 1;
            text-align: center;
            cursor: pointer;
        }

        .active {
            background-color: #87ceeb;
        }
     
        .main{
            height: 200px;
            background-color: greenyellow;
        }
        .yc{
            display: none;
        }
    </style>
</head>

<body>
    <div class="box">
        <ul class="head">
            <li class="active">标题一</li>
            <li>标题二</li>
            <li>标题三</li>
        </ul>
    </div>
    <div class="main">
            <ul class="mainT">
                <li class="mainL">内容一</li>
                <li class="yc mainL">内容二</li>
                <li class="yc mainL">内容三</li>
            </ul>
    </div>

    <script>
        let list = document.querySelectorAll(".head > li");
        let main = document.querySelector(".main");
        let lastIndex = 0; // 设置上一步操作的下标
        let mainL=document.querySelectorAll(".mainT>li")

        //  head容器每个li绑定事件
        list.forEach(function (item, index) {
            item.onclick = function () {
                list[lastIndex].classList.remove("active");
                item.classList.add("active");
                lastIndex = index;
                

            mainL.forEach(function(item,index){
                console.log(item);
                if(index==lastIndex){
                main.innerText = item.innerText;
                }
            })
                
            }
        });
    </script>
</body>

</html>